﻿using System;
using System.Data.SqlClient;
using BikeBouncer.BusinessLayer.Entities;
using Devv.Core.UniDataTier;

namespace BikeBouncer.BusinessLayer
{
	public partial class DataAccess
	{
		#region Get

		public static SqlDataReader Maker_Get(Int32 id)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@MakerId", id);

			return (SqlDataReader)db.ExecuteReader("upMaker_Get");
		}

		public static SqlDataReader Maker_Get(String title)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@Title", title);

			return (SqlDataReader)db.ExecuteReader("upMaker_Get");
		}

		public static SqlDataReader Maker_Fetch(String keywords)
		{
			keywords = DataParser.ParseKeywords(keywords);

			if (String.IsNullOrEmpty(keywords))
			{
				return null;
			}

			DataHandler db = new DataHandler();

			db.AddParameter("@Keywords", keywords);

			return (SqlDataReader)db.ExecuteReader("upMaker_Get");
		}

		#endregion

		#region Insert and update

		public static void Maker_Upsert(Maker maker)
		{
			using (DataHandler db = new DataHandler())
			{
				db.AddParameter("@Title", maker.Title);
				db.AddParameter("@Website", maker.Website, true);
				db.AddParameter("@Active", maker.Active);

				if (maker.Id > 0)
				{
					db.AddParameter("@MakerId", maker.Id);

					db.ExecuteScalar("upMaker_Update");
				}
				else
				{
					maker.Id = Convert.ToInt32(db.ExecuteScalar("upMaker_Insert"));
				}
			}
		}

		#endregion
	}
}